/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.auth.domain.exception;

import com.iwindplus.boot.domain.exception.CommonException;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.OAuth2Error;

import java.util.Objects;

/**
 * 自定义oauth2异常
 *
 * @author zengdegui
 * @since 2024/05/22 22:20
 */
@Getter
public class CustomOauth2AuthenticationException extends OAuth2AuthenticationException implements CommonException {

    /**
     * 业务状态码.
     */
    private final Integer bizStatus;

    /**
     * 业务编码.
     */
    private final String bizCode;

    /**
     * 业务信息.
     */
    private final String bizMessage;

    public CustomOauth2AuthenticationException(CommonException code) {
        super(new OAuth2Error(code.getBizCode()), code.getBizMessage());
        this.bizStatus = Objects.nonNull(code.getBizStatus()) ? code.getBizStatus() : HttpStatus.INTERNAL_SERVER_ERROR.value();
        this.bizCode = code.getBizCode();
        this.bizMessage = code.getBizMessage();
    }

    public CustomOauth2AuthenticationException(String bizCode, String msg) {
        super(new OAuth2Error(bizCode), msg);
        this.bizStatus = HttpStatus.INTERNAL_SERVER_ERROR.value();
        this.bizCode = bizCode;
        this.bizMessage = msg;
    }
}